﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using CredentialsManager.Helpers;
using CredentialsManager.Properties;

namespace CredentialsManager.ViewModels
{
    public class RolesForUserViewModel : ViewModelBase
    {
        #region Private classes

        private class WorkerInput
        {
            public string Application { get; set; }
            public string User { get; set; }
        }

        #endregion

        #region Private member variables

        private BackgroundWorker m_PopulateWorker;
        private ReadOnlyCollection<string> m_Roles;
        private string m_User;

        #endregion

        #region Constructor

        public RolesForUserViewModel(string application, string user)
        {
            this.DisplayName = string.Format(Resources.RolesForUserWindowTitle.ToString(),
                user);
            m_PopulateWorker = new BackgroundWorker();
            m_PopulateWorker.Initialise(m_PopulateWorker_DoWork,
                m_PopulateWorker_RunWorkerCompleted, null, false);

            this.User = user;
            var input = new WorkerInput() { Application = application, User = user };
            this.IsLoading = true;
            m_PopulateWorker.RunWorkerAsync(input);
        }

        #endregion

        #region Background worker handlers

        private void m_PopulateWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var input = e.Argument as WorkerInput;
            Debug.Assert(input != null);

            var proxy = ProxyFactory.Create<RoleManagerClient, IRoleManager>(ServiceAddressList.CredentialsService);

            try
            {
                var roles = proxy.GetRolesForUser(input.Application, input.User);
                e.Result = roles;
            }
            finally
            {
                ProxyFactory.Cleanup<RoleManagerClient, IRoleManager>(proxy);
            }
        }

        private void m_PopulateWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                ErrorHandler.HandleNonSpecificWCFException(e.Error,
                    this.DisplayName);
            }
            else
            {
                var roles = e.Result as ObservableCollection<string>;
                Debug.Assert(roles != null);
                this.Roles = new ReadOnlyCollection<string>(roles);
            }

            this.IsLoading = false;
        }

        #endregion

        #region Public properties

        public string User
        {
            get
            {
                return m_User;
            }
            private set
            {
                m_User = value;
                RaisePropertyChanged(MethodBase.GetCurrentMethod().GetPropertyName());
            }
        }

        public ReadOnlyCollection<string> Roles
        {
            get
            {
                return m_Roles;
            }
            private set
            {
                m_Roles = value;
                RaisePropertyChanged(MethodBase.GetCurrentMethod().GetPropertyName());
            }
        }

        #endregion
    }
}
